package com.bzkj.basicinfo.service.wms.impl;

import com.bzkj.basicinfo.domain.ncc.TableXXXSku;
import com.bzkj.basicinfo.domain.wms.WmsQaHistoryEntity;
import com.bzkj.basicinfo.domain.wms.WmsStoStockEntity;
import com.bzkj.basicinfo.domain.wms.WmsStoStockLimitationEntity;
import com.bzkj.basicinfo.mapper.*;
import com.bzkj.basicinfo.service.wms.WmsStoStockLimitationTypeService;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import com.bzkj.common.core.domain.entity.SysUser;
import com.bzkj.common.utils.SecurityUtils;
import com.bzkj.common.utils.StringUtils;
import com.bzkj.common.utils.uuid.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author wl
 * @description 针对表【wms_sto_stock_limitation_type(限制类型)】的数据库操作Service实现
 * @createDate 2025-01-02 09:51:37
 */
@Service
public class WmsStoStockLimitationTypeServiceImpl
        implements WmsStoStockLimitationTypeService
{

    @Autowired
    private WmsStoStockLimitationTypeMapper wmsStoStockLimitationTypeMapper;

    @Autowired
    private WmsStoStockLimitationMapper wmsStoStockLimitationMapper;

    @Autowired
    private BasicBillsTypeMapMapper basicBillsTypeMapMapper;

    @Autowired
    private TableXXXSkuMapper tableXXXSkuMapper;

    @Autowired
    private WmsStoStockMapper wmsStoStockMapper;

    @Autowired
    private WmsQaHistoryMapper wmsQaHistoryMapper;


    /**
     * 获取所有的限制类型
     *
     * @return
     */
    @Override
    public R findTypeList()
    {
        return R.ok(wmsStoStockLimitationTypeMapper.findTypeList());
    }

    /**
     * 获取限制类型信息
     *
     * @param pkMaterial
     * @param pkBatchCode
     * @return
     */
    @Override
    public R findLimitationInfo(String pkMaterial, String pkBatchCode)
    {
        HashMap<String, Object> map = new HashMap<>();
        map.put("单据类型冻结", null);
        map.put("追溯码限制", null);
        map.put("数量冻结",null);


        List<HashMap<String, Object>> list = wmsStoStockLimitationMapper.getEnableStatus0All(pkMaterial, pkBatchCode);
        if(! list.isEmpty())
            map.put("单据类型冻结", list);

        //数量冻结
        List<HashMap<String, Object>> statusNumLimit = wmsStoStockLimitationMapper.selectAllListByType(1, pkMaterial, pkBatchCode);
        if (!statusNumLimit.isEmpty()) {
            List<HashMap<Object, Object>> mapList = new ArrayList<>();
            statusNumLimit.forEach(item -> {
                Object id = item.get("id");
                Object statusPk = item.get("bill_status_pk");//库存状态
                Object limitQuantity = item.get("limit_quantity");//冻结数量
                if (statusPk.equals("1001A110000000002T09") ) {
                    HashMap<Object, Object> map3 = new HashMap<>();
                    map3.put("PK_STORESTATE", "1001A110000000002T09");
                    map3.put("name", "合格");
                    map3.put("num", limitQuantity);
                    map3.put("id", id);
                    mapList.add(map3);
                }
                if (statusPk.equals("1001ZZ1000000002XCX4") ) {
                    HashMap<Object, Object> map1 = new HashMap<>();
                    map1.put("PK_STORESTATE", "1001ZZ1000000002XCX4");
                    map1.put("name", "不合格");
                    map1.put("num", limitQuantity);
                    map1.put("id", id);
                    mapList.add(map1);
                }
                if (statusPk.equals("1001A110000000002T08")) {
                    HashMap<Object, Object> map2 = new HashMap<>();
                    map2.put("PK_STORESTATE", "1001A110000000002T08");
                    map2.put("name", "待检");
                    map2.put("num", limitQuantity);
                    map2.put("id", id);
                    mapList.add(map2);
                }
                if (statusPk.equals("1001B1100000008N4LKD") ) {
                    HashMap<Object, Object> map4 = new HashMap<>();
                    map4.put("PK_STORESTATE", "1001B1100000008N4LKD");
                    map4.put("name", "限制合格");
                    map4.put("num", limitQuantity);
                    map4.put("id", id);
                    mapList.add(map4);
                }
                if (statusPk.equals("1001B110000000FNCCLS") ) {//研究用  生产环境pk:1001B110000000FNCCLS
                    HashMap<Object, Object> map5 = new HashMap<>();
                    map5.put("PK_STORESTATE", "1001ZZ10000000ABO2GR");
                    map5.put("name", "研究用");
                    map5.put("num", limitQuantity);
                    map5.put("id", id);
                    mapList.add(map5);
                }
                if (statusPk.equals("1001B110000000CMZGQ2") ) {//合格不予上市  生产环境pk:1001B110000000CMZGQ2
                    HashMap<Object, Object> map6 = new HashMap<>();
                    map6.put("PK_STORESTATE", "1001B110000000CMZGQ2");
                    map6.put("name", "合格不予上市");
                    map6.put("num", limitQuantity);
                    map6.put("id", id);
                    mapList.add(map6);
                }
                if (statusPk.equals("1001B1100000008HXPEP") ) {
                    HashMap<Object, Object> map7 = new HashMap<>();
                    map7.put("PK_STORESTATE", "1001B1100000008HXPEP");
                    map7.put("name", "待处理");
                    map7.put("num", limitQuantity);
                    map7.put("id", id);
                    mapList.add(map7);
                }
                if (StringUtils.isBlank(statusPk.toString()) ) {
                    HashMap<Object, Object> map8 = new HashMap<>();
                    map8.put("PK_STORESTATE", "");
                    map8.put("name", "无库存状态");
                    map8.put("num", limitQuantity);
                    map8.put("id", id);
                    mapList.add(map8);
                }
            });
            if (!mapList.isEmpty()) {
                map.put("数量冻结", mapList);
            }
        }
//        List<HashMap<String, Object>> list1 = wmsStoStockLimitationMapper.getEnableStatus3All(pkMaterial, pkBatchCode);
//        if(! list1.isEmpty()) {
        String tableName = "table_" + pkMaterial + "_sku";
        boolean tableExist = tableXXXSkuMapper.isTableExist(tableName);
        if (tableExist) {
            List<TableXXXSku> tableXXXSkuListByBatchCode = tableXXXSkuMapper.findTableXXXSkuListByBatchCode1(tableName, pkBatchCode);
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("skuList", tableXXXSkuListByBatchCode);
//                map1.put("limitation", list1);
            map.put("追溯码限制", map1);
        }
//        }

        return R.ok(map);
    }

    @Override
    public AjaxResult putBillsTypeEnableStatus(String id)
    {
        //根据id查询冻结数据
        WmsStoStockLimitationEntity one = wmsStoStockLimitationMapper.selectOneById(id);
        if (Objects.nonNull(one)) {
            //状态主键，根据状态主键判断冻结的是什么状态
            Integer limitQuantity = one.getLimitQuantity();//冻结数量
            WmsStoStockEntity stoStockEntity = wmsStoStockMapper.finWmsStoStockOne(one.getPkMaterial(), one.getPkBatchcode(),one.getCwarehouseId(),one.getProductName());
            if (Objects.nonNull(stoStockEntity)) {
                Long freezeNum = stoStockEntity.getFreezeNum();//已冻结总数
                stoStockEntity.setFreezeNum(freezeNum - limitQuantity);
                //修改库存数量
                wmsStoStockMapper.update(stoStockEntity);
                //操作记录
                WmsQaHistoryEntity historyEntity = new WmsQaHistoryEntity();
                String s = UUID.randomUUID().toString().replace("-", "");
                historyEntity.setWmsQaId(s);
                historyEntity.setPkMaterial(one.getPkMaterial());
                historyEntity.setPkBatchcode(one.getPkBatchcode());
                SysUser user = SecurityUtils.getLoginUser().getUser();
                historyEntity.setOperator(user.getUserId().toString());
                historyEntity.setOperatorName(user.getUserName());
                historyEntity.setOperattime(new Date());
                historyEntity.setCwarehouseId(stoStockEntity.getCwarehouseId());
                historyEntity.setCproductorId(stoStockEntity.getPkProduct());
                historyEntity.setType("4");
                historyEntity.setSonType("2");
                wmsQaHistoryMapper.saveWmsQaHistory(historyEntity);
            }
        }
        // 关闭限制
        wmsStoStockLimitationMapper.deleteById(id);
        return AjaxResult.success("关闭成功");
    }

    @Transactional
    @Override
    public R putBatchEnableStatus(String id, List<String> batchId, String pkMaterial, String pkBatchcode)
    {
        if(batchId.isEmpty()) return R.fail("请选择追溯码");

        tableXXXSkuMapper.updateEnable(batchId, "table_" + pkMaterial + "_sku", pkBatchcode);

        List<TableXXXSku> list = tableXXXSkuMapper.findTableXXXSkuListByBatchCode1("table_" + pkMaterial + "_sku", pkBatchcode);
        if(list.isEmpty())
            wmsStoStockLimitationMapper.updateEnable(id);

        return R.ok("关闭成功");
    }
}




